//
//  MyShopTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/1.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MyShopTabVC.h"
#import "ShopCell.h"
#import "ServiceCell.h"
#import "DemandCell.h"
#import "WriteRequirementTitleCell.h"
#import "ShopOnlineOfflineCell.h"
#import "EvaluationCell.h"
#import "ServiceEvaluationModel.h"
#import "ServiceModel.h"
#import "ShopSubModel.h"
#import "HomeModel.h"
#import "ShopModel.h"
@interface MyShopTabVC ()<DemandCellDelegate,UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSMutableArray *evaluationArray;
@property(nonatomic,strong) NSMutableArray *demandArray;
@property(nonatomic,strong) NSMutableArray *dataMuta;
@property(nonatomic,strong) NSMutableArray *serviceArray;
@property(nonatomic,strong) NSMutableArray *shopArray;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,assign) NSInteger page;
@property(nonatomic,assign) NSInteger row;
@property(nonatomic,strong) NSString *associationtype;
@property(nonatomic,strong) NSString *associationtypesub1;
@property(nonatomic,strong) NSString *associationtypesub2;
@property(nonatomic,strong) NSString *commonindustry;
@property(nonatomic,strong) NSString *industry;
@property(nonatomic,assign) NSInteger type;
@property(nonatomic,assign) NSInteger order;
@property(nonatomic,strong) MCButton *preBtn;
@property(nonatomic,assign) NSInteger btnTag;
@property(nonatomic,strong) UIButton *sureBtn;
@property(nonatomic,strong) MJRefreshAutoNormalFooter *footer;

@end
static NSString *identifierShop = @"shopCell";
static NSString *identifierShopOnOFF = @"shopOnOffCell";
static NSString *identifierWriteRequirementTitleCell = @"WriteRequirementTitleCell";
static NSString *identifierEvaluation = @"evaluationCell";
static NSString *identifierService = @"ServiceCell";
static NSString *identifierNeed = @"DemandCell";
@implementation MyShopTabVC
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kVWidth, kUIScreen.size.height-50-kNavAndStatusHight-kTabBarHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
-(NSMutableArray *)shopArray
{
    if (!_shopArray) {
        _shopArray = [NSMutableArray array];
    }
    return _shopArray;
}
-(NSMutableArray *)dataMuta
{
    if (!_dataMuta) {
        _dataMuta = [NSMutableArray array];
        
    }
    return _dataMuta;
}
-(NSMutableArray *)demandArray
{
    if (!_demandArray) {
        _demandArray = [NSMutableArray array];
        
    }
    return _demandArray;
}
-(NSMutableArray *)serviceArray
{
    if (!_serviceArray) {
        _serviceArray = [NSMutableArray array];
        
    }
    return _serviceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.navigationItem.title = @"我的店铺";
    self.view.backgroundColor = [UIColor whiteColor];
//    self.sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width*0.05, kUIScreen.size.height-50-kNavAndStatusHight-kTabBarHeight, kUIScreen.size.width*0.9, 44)];
//    [self.view addSubview:self.sureBtn];
//    [self.sureBtn addTarget: self action:@selector(nextPage:) forControlEvents:UIControlEventTouchUpInside];
//    [self.sureBtn setBackgroundColor:kOrangeBg];
//    self.sureBtn.layer.masksToBounds = YES;
//    self.sureBtn.layer.cornerRadius = 8;
//    self.sureBtn.titleLabel.font = [UIFont systemFontOfSize:16];
//    [self.sureBtn setTitle:@"创建新服务" forState:UIControlStateNormal];
//    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopCell" bundle:nil] forCellReuseIdentifier:identifierShop];
    [self.tableView registerNib:[UINib nibWithNibName:@"WriteRequirementTitleCell" bundle:nil] forCellReuseIdentifier:identifierWriteRequirementTitleCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"ShopOnlineOfflineCell" bundle:nil] forCellReuseIdentifier:identifierShopOnOFF];
    [self.tableView registerNib:[UINib nibWithNibName:@"DemandCell" bundle:nil] forCellReuseIdentifier:identifierNeed];
    [self.tableView registerNib:[UINib nibWithNibName:@"ServiceCell" bundle:nil] forCellReuseIdentifier:identifierService];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(demandselectDemandByUid:) name:@"demandselectDemandByUid" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(associationSearch:) name:@"associationSearch" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(qingqiusibai:) name:@"qingqiusibai" object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectShopByUid:) name:@"selectShopByUid" object:nil];
    [self.tableView registerNib:[UINib nibWithNibName:@"EvaluationCell" bundle:nil] forCellReuseIdentifier:identifierEvaluation];
    self.page = 1;
    self.row = 1;
    self.btnTag = 1;
    [self reloadInternetShop];
}
#pragma mark-代理:店铺
-(void)selectShopByUid:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    NSDictionary *date = [dict objectForKey:@"date"];
    ShopModel *model = [ShopModel shopModelWithDictionary:date];
    [self.shopArray addObject:model];
//    [self.tableView reloadData];
    [self reloadInternetData];
}
#pragma mark-代理:服务,需求列表
-(void)associationSearch:(NSNotification *)info
{
    
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
    if (date.count) {
        for (NSDictionary *dict in date) {
            ServiceModel *model = [ServiceModel mj_objectWithKeyValues:dict];
            [self.dataMuta addObject:model];
        }
        
    }else {
    }
    if (self.dataMuta.count) {
        self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
        self.tableView.mj_footer = self.footer;
        [self.footer setTitle:@"~~~ 我是有底线的 ~~~" forState:MJRefreshStateNoMoreData];
        self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
        self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
        if (date.count&&date.count< 10) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
    }else {
        [self.tableView.mj_footer removeFromSuperview];
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}
-(void)demandselectDemandByUid:(NSNotification *)info
{
    
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    NSArray *date = [dict objectForKey:@"date"];
    if (date.count) {
        for (NSDictionary *dict in date) {
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dict];
            [self.dataMuta addObject:model];
        }
        XLog(@"self.dataMuta:%@",self.demandArray);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }else {
        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    }
    [self.tableView reloadData];
    [WSProgressHUD dismiss];
}
-(void)qingqiusibai:(NSNotification *)info
{
    NSDictionary *dict = info.userInfo;
    XLog(@"dict:%@",dict);
    self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
    
}
-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(void)reloadInternetShop {
    self.dict = @{
                  @"uid":@"1"
                  };
    [CustomeManager POSTWithURLIndex:16 parameters:self.dict success:^(NSDictionary *dict) {
        
    } failure:^(NSError *error) {
        
    }];
}
-(void)reloadInternetData {
    
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    ShopModel *model = self.shopArray[0];
    ShopSubModel *subModel = model.data[0];
    NSString *uids = [NSString stringWithFormat:@"%ld",subModel.uid];
    if (self.btnTag == 1) {
        NSString *types = [NSString stringWithFormat:@"%ld",self.type];
        NSString *orders = [NSString stringWithFormat:@"%ld",self.order];
        NSString *pages = [NSString stringWithFormat:@"%ld",self.page];
        NSString *rows = [NSString stringWithFormat:@"%ld",self.row];
        self.dict = @{
                      @"uid":uids,
                      @"associationtype":@"",
                      @"associationtypesub1":@"",
                      @"associationtypesub2":@"",
                      @"commonindustry":@"",
                      @"industry":@"",
                      @"type":types,
                      @"order":orders,
                      @"page":pages,
                      @"rows":rows
                      };
        XLog(@"self.dict:%@",self.dict);
        [CustomeManager POSTWithURLIndex:24 parameters:self.dict success:^(NSDictionary *dict) {

        } failure:^(NSError *error) {

        }];
    }else {
        NSString *pages = [NSString stringWithFormat:@"%ld",self.page];
        NSString *rows = [NSString stringWithFormat:@"%ld",self.row];

        self.dict = @{
                      @"uid":uids,
                      @"pageNum":pages,
                      @"pageSize":rows
                      };
        XLog(@"self.dict:%@",self.dict);
        [CustomeManager POSTWithURLIndex:30 parameters:self.dict success:^(NSDictionary *dict) {

        } failure:^(NSError *error) {

        }];

    }
    
    
}
-(void)reloadMore
{
    self.page ++;
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    [self reloadInternetData];
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else {
        if (self.btnTag == 1) {
            return self.dataMuta.count+1;
        }else {
            return self.dataMuta.count+1;
        }
        
        
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView  setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.section == 0) {
        ShopCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierShop forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else {
        if (indexPath.row == 0) {
            WriteRequirementTitleCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierWriteRequirementTitleCell forIndexPath:indexPath];
            cell.titleL.text = @"重要信息说明";
            return cell;
        }else  {
            if (self.btnTag == 1) {
                ServiceCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierService forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                if (self.dataMuta.count) {
                    ServiceModel *model = self.dataMuta[indexPath.row-1];
                    cell.serviceModel = model;
                }
                return cell;
                
            }else {
                DemandCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierNeed forIndexPath:indexPath];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.indexPath = indexPath;
                if (self.dataMuta.count) {
                    HomeModel *model = self.dataMuta[indexPath.row-1];
                    cell.delegate = self;
                    cell.homeModel = model;
                }
                cell.delegate = self;
                return cell;
            }
        }
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 180;
    }else  {
        if (indexPath.row == 0) {
            return 40;
        }else {
            return 144;
        }
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 1)];
    headerView.backgroundColor = [UIColor whiteColor];
    CGFloat w=kUIScreen.size.width/4;
    if (section == 1) {
        for (int i = 0; i < 3; i++) {
            MCButton *btn = [[MCButton alloc]initWithFrame:CGRectMake(kUIScreen.size.width/3*i+(kUIScreen.size.width/3-w)/2, 10, w, 30)];
            [headerView addSubview:btn];
            btn.tag = i;
            [btn addTarget: self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
            NSArray *titles = @[@"搜索",@"服务列表",@"需求列表"];
            [btn setTitle:titles[i] forState:UIControlStateNormal];
            if (self.btnTag == 1) {
                if (i == 0) {
                    [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:16];
                    [btn setImage:[UIImage imageNamed:@"icon_15"] forState:UIControlStateNormal];
                    btn.buttonStyle = imageLeft;
                    btn.layer.masksToBounds = YES;
                    btn.layer.cornerRadius = 15;
                    btn.layer.borderColor  = kOrangeBg.CGColor;
                    btn.layer.borderWidth  = 2;
                }else  if (i == 1) {
                    [btn setTitleColor:kLabel16Color forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:14];
                    btn.buttonStyle = imageBottom;
                    [btn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
                    self.preBtn = btn;
                }else {
                    [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:14];
                    btn.buttonStyle = imageBottom;
                    [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
                }
                
            }else {
                if (i == 0) {
                    [btn setTitleColor:kOrangeBg forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:16];
                    [btn setImage:[UIImage imageNamed:@"icon_15"] forState:UIControlStateNormal];
                    btn.buttonStyle = imageLeft;
                    btn.layer.masksToBounds = YES;
                    btn.layer.cornerRadius = 15;
                    btn.layer.borderColor  = kOrangeBg.CGColor;
                    btn.layer.borderWidth  = 2;
                }else  if (i == 1) {
                    [btn setTitleColor:kLabel153Color forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:14];
                    btn.buttonStyle = imageBottom;
                    [btn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
                }else {
                    [btn setTitleColor:kLabel16Color forState:UIControlStateNormal];
                    btn.titleLabel.font = [UIFont systemFontOfSize:14];
                    btn.buttonStyle = imageBottom;
                    [btn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
                    self.preBtn = btn;
                }
            }
        }
    }
    
    return headerView;
    
}
-(void)clickBtn:(MCButton *)clickBtn
{
    if (clickBtn.tag == 0) {
        //        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
        //        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    }else if (clickBtn.tag == 1) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [self.sureBtn setTitle:@"创建新服务" forState:UIControlStateNormal];
    }else {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:1];
        //        [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
        [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
        [self.sureBtn setTitle:@"创建新需求" forState:UIControlStateNormal];
    }
    if (clickBtn.tag != 0) {
        if (self.preBtn == clickBtn) {
            [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
        }else {
            [clickBtn setImage:[ImageColorHelper createImageWithColor:kOrangeBg] forState:UIControlStateNormal];
            [clickBtn setTitleColor:kLabel16Color forState:UIControlStateNormal];
            [self.preBtn setTitleColor:kLabel153Color forState:UIControlStateNormal];
            [self.preBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        }
        self.preBtn = clickBtn;
        self.btnTag  = clickBtn.tag;
    }
    self.dataMuta = nil;
    self.demandArray = nil;
    self.serviceArray = nil;
    self.page = 1;
    self.row = 1;
    [self reloadInternetData];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 50;
    }else {
        return 0.1;
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,1)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 1) {
        return 30;
    }else {
        return 0.1;
    }
}
#pragma mark-代理---下一步
-(void)nextPage:(UIButton *)nextPage
{
//    NSMutableString * str= [[NSMutableString alloc] initWithFormat:@"tel:%ld",self.shopModel.phone];
//    UIWebView * callWebview = [[UIWebView alloc] init];
//    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
//    [self.view addSubview:callWebview];
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

@end
